﻿using BLL;
using Config;
using EncryptHelper;
using gfLibrary;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IYAKASAPortal
{
    public partial class Admin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            { 
                btnSearch_Click(sender, e);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string strSql = "select ID,EMPLOYEE_NO,EMPLOYEE_ID,EMPLOYEE_PWD,EMPLOYEE_POWER,LICENSE_NAME,EMPLOYEE_NAME,REG_DATE from EMPLOYEE_USER";
            string strKeywords = txtKeywords.Text;
            if (strKeywords.Length > 0)
            {
                strSql += "  where ( ";
                strSql += " EMPLOYEE_ID like '%" + strKeywords + "%'";
                strSql += " or EMPLOYEE_NO like '%" + strKeywords + "%'";
                strSql += " or EMPLOYEE_NAME like '%" + strKeywords + "%')";
            }
            strSql += " order by ID asc";

            DataTable m_dtTable = new DataTable();
            GreatPager1.BindDataTablePage(ref m_dtTable, AppConfig.g_strConnectionStringSqlClient, strSql, 20);

            strSql += " order by ID desc";

            userList.DataSource = m_dtTable;
            userList.DataBind();

            m_dtTable.Dispose();
        }

        protected void GreatPager1_PageIndexChanging(object sender, gfPage.PagerEventArgs e)
        {
            GreatPager1.PageIndex = e.NewPageIndex;

            DataTable m_dtTable = GreatPager1.BindDataTablePage();
            userList.DataSource = m_dtTable;
            userList.DataBind();
        }

        protected string getPowerName(string power)
        {
            string tempName="";
            string result = "";
            if (power.Length == 1)
            {
                tempName = power;
            }
            else
            {
                tempName = Encrypt.AESDecrypt(power);
            }

            switch (tempName)
            {
                case "0":
                    result = "厂家人员";
                    break;
                case "1":
                    result = "监督部门";
                    break;
                case "2":
                    result = "站长";
                    break;
                case "3":
                    result = "管理员";
                    break;
                case "4":
                    result = "设备管理员";
                    break;
                case "5":
                    result = "操作员";
                    break;
                default:
                    result = "无权限";
                    break;
            }
            return result;
        }

        protected string getLicenseName(string license)
        {
            string result = "";
            string[] arr = license.Split(',');
            for (int i = 0; i < license.Split(',').Length; i++)
            {
                result +=getLicense(arr[i])+" ";
            }
            return result;
        }

        protected string getLicense(string num)
        {
            string result="";
            switch (num)
            {
                case "01":
                    result = "登录员";
                    break;
                case "02":
                    result = "引车员";
                    break;
                case "03":
                    result = "外检员";
                    break;
                case "04":
                    result = "底盘员";
                    break;
                case "05":
                    result = "尾气操作员";
                    break;
                case "06":
                    result = "协作操作员";
                    break;
                case "07":
                    result = "审核员";
                    break;
                case "08":
                    result = "授权签字员";
                    break;
                case "09":
                    result = "微机操作员";
                    break;
                case "10":
                    result = "路试检验员";
                    break;
                case "11":
                    result = "收费员";
                    break;
                case "12":
                    result = "系统管理员";
                    break;
                case "13":
                    result = "设备管理员";
                    break;
                case "14":
                    result = "站长";
                    break;
            }
            return result;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string strUniqueKey = Request.Form["txtUniqueKey"];

            if ((strUniqueKey == null) || (strUniqueKey.Length == 0) || (gfValidate.Is_Integer(strUniqueKey) == false))
            {
                gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "请选择一条记录！");
                return;
            }
            EMPLOYEE_USER_BLL user = new EMPLOYEE_USER_BLL();
            int userId = int.Parse(strUniqueKey);
            string strName = user.GetModel(userId).EMPLOYEE_NAME;
            if (string.IsNullOrEmpty(strName) || "admin" == strName)
            {
                gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "你不能删除admin账号。");
                return;
            }
            user.Delete(userId);
            Response.Redirect(Request.Url.ToString());
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            string strUniqueKey = Request.Form["txtUniqueKey"];

            if ((strUniqueKey == null) || (strUniqueKey.Length == 0) || (gfValidate.Is_Integer(strUniqueKey) == false))
            {
                gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "请选择一条记录！");
                return;
            }
            Response.Redirect("Update.aspx?id=" + strUniqueKey);
        }

        protected void btnSearchAll_Click(object sender, EventArgs e)
        {
            txtKeywords.Text = "";
            btnSearch_Click(sender, e);
        }
    }
}